import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { asnycRoute, constantRoute } from '@/router/routers'
import cloneDeep from 'lodash/cloneDeep'
import { reqLogout } from '@/api/user/index'
import { filterAsyncRoute } from '@/utils/axyncRouter'
export const useUserStore = defineStore(
  'user',
  () => {
    // 用户登录
    const user = ref<User>()
    const setUser = (u: User) => {
      user.value = u
    }
    // 用户登录 end

    // 用户信息
    const userinfo = ref<any>()
    const delUser = async () => {
      user.value = undefined
      userinfo.value = undefined
      menuRoutes.value = undefined
      await reqLogout()
    }
    // 用户信息 end

    // 动态路由
    const menuRoutes = ref<any>()
    menuRoutes.value = cloneDeep(constantRoute)
    // menuRoutes.value = filterAsyncRoute(
    //   cloneDeep(asyncRoute),
    //   userinfo.value.routes
    // )

    const buttons = []
    // 动态路由 end

    return { user, setUser, delUser, userinfo, menuRoutes, buttons }
  },
  // 选择持久化的配置
  {
    persist: true
  }
)
